package com.oms.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oms.dao.AcknowledgementDao;
import com.oms.dao.BaseDao;
import com.oms.domain.Acknowledgement;
import com.oms.domain.User;
import com.oms.service.AcknowledgementService;

@Transactional
@Service("acknowledgementService")
public class AcknowledgementServiceImpl extends BaseServiceImpl<Acknowledgement> implements AcknowledgementService {
	
    @Resource(name = "acknowledgementDao")
    public void setDao(BaseDao<Acknowledgement> dao) {  
        super.setDao(dao);  
    }  
	
	@Resource
	private AcknowledgementDao acknowledgementDao;


	@Fetch(FetchMode.JOIN)
	@Override
	public List<Acknowledgement> getMessageList(User receiver, Timestamp startTime,
			Timestamp endTime) {
		//如果没有选时间段，则显示将时间段限制为不受限制
		if(startTime==null){
			startTime=new Timestamp(0);
		}
		if(endTime==null){
			endTime=new Timestamp(System.currentTimeMillis()+1000*3600);
		}
		
		String hql="from Acknowledgement acknowledgement fetch all properties  where acknowledgement.user=?0 and acknowledgement.message.sendTime between ?1 and ?2 and acknowledgement.deleteTag=0";
		return acknowledgementDao.list(hql, receiver,startTime,endTime);

		
/*		List<Acknowledgement> ackList= acknowledgementDao.list(hql, receiver,startTime,endTime);
		List<Message> messageList=new ArrayList<Message>();
		for(Acknowledgement ack : ackList){
			messageList.add(ack.getMessage());
		}
		return messageList;*/
	}

	@Fetch(FetchMode.JOIN)
	@Override
	public List<Acknowledgement> getMessageListWithPage(User receiver,
			Integer firstResult, Integer maxResults, Timestamp startTime,
			Timestamp endTime) {
		//如果没有选时间段，则显示将时间段限制为不受限制
		if(startTime==null){
			startTime=new Timestamp(0);
		}
		if(endTime==null){
			endTime=new Timestamp(System.currentTimeMillis()+1000*3600);
		}
		
		String hql="from Acknowledgement acknowledgement fetch all properties  where acknowledgement.user=?0 and acknowledgement.message.sendTime between ?1 and ?2 and acknowledgement.deleteTag=0";
		return acknowledgementDao.listWithPage(hql, firstResult, maxResults, receiver,startTime,endTime);
	}
	
	@Override
	public List<Acknowledgement> getUnReadMsgListWithPage(User receiver,
			Integer firstResult, Integer maxResults, Timestamp startTime,
			Timestamp endTime) {
		//如果没有选时间段，则显示将时间段限制为不受限制
		if(startTime==null){
			startTime=new Timestamp(0);
		}
		if(endTime==null){
			endTime=new Timestamp(System.currentTimeMillis()+1000*3600);
		}
		
		String hql="from Acknowledgement acknowledgement fetch all properties  where acknowledgement.user=?0 and "
				+ "acknowledgement.message.sendTime between ?1 and ?2 and acknowledgement.isRead=0 and acknowledgement.deleteTag=0";
		return acknowledgementDao.listWithPage(hql, firstResult, maxResults, receiver,startTime,endTime);
	}

	@Override
	public List<Acknowledgement> getReadedMsgListWithPage(User receiver,
			Integer firstResult, Integer maxResults, Timestamp startTime,
			Timestamp endTime) {
		//如果没有选时间段，则显示将时间段限制为不受限制
		if(startTime==null){
			startTime=new Timestamp(0);
		}
		if(endTime==null){
			endTime=new Timestamp(System.currentTimeMillis()+1000*3600);
		}
		
		String hql="from Acknowledgement acknowledgement fetch all properties  where acknowledgement.user=?0 and "
				+ "acknowledgement.message.sendTime between ?1 and ?2 and acknowledgement.isRead=1 and acknowledgement.deleteTag=0";
		return acknowledgementDao.listWithPage(hql, firstResult, maxResults, receiver,startTime,endTime);
	}
	
	@Override
	public Integer getUnReadMsgCount(User receiver) {
		String hql="select count(*) from Acknowledgement acknowledgement where acknowledgement.user=?0 and acknowledgement.isRead=0 and "
				+ "acknowledgement.deleteTag=0";		
		return acknowledgementDao.count(hql, receiver);
	}


	@Override
	public void deleteMsg(Integer id, User user) {
		Acknowledgement acknowledgement=acknowledgementDao.findById(id);
		//判断找到的信息是不是空
		if(acknowledgement==null || acknowledgement.getDeleteTag()==true){
			throw new RuntimeException("0");	//找不到信息异常
		}
		//判断传入用户是否有权限删除
		if(user==null || !user.getUserId().equals(acknowledgement.getUser().getUserId())){
			throw new RuntimeException("1");	//无权限异常
		}
		acknowledgement.setDeleteTag(true);
		acknowledgementDao.update(acknowledgement);
		System.out.println("删除成功！");
	}
	
	
	@Override
	public Acknowledgement receiverReadMsg(Integer id, User user) {
		Acknowledgement acknowledgement=acknowledgementDao.findById(id);
		//找到的信息是否为空
		if(acknowledgement==null || acknowledgement.getDeleteTag()==true) {
			return null;	//找不到,返回空
		}
		//传入的用户是否有权限查阅信息
		if(user==null || !user.getUserId().equals(acknowledgement.getUser().getUserId())){
			throw new RuntimeException("1");	//无权限异常
		}
		if(acknowledgement.getIsRead()==false){
			acknowledgement.setIsRead(true);
			acknowledgementDao.update(acknowledgement);
		}
		return acknowledgement;
	}


	
}
